import { Player } from '../compoment/player';
import { PosInfo, GameState, PlayerInput, ServerMessage, playerState } from '../define/GameDefine';
import GameSet from '../define/GameSet';
import { UICommandKey } from '../define/UICommand';
import ViewMgr from '../manager/ViewMgr';
import MapModel from '../model/MapModel';
import { Item } from '../object/Item';
/**
 * 战场控制类 存一些控制变量 或者方法
 */
import { WebSocketClient } from '../sockets/WebSocketClient';
import { gameData } from '../tool/GameData';
import RandomTool from '../tool/RandomTool';
export default class GameCtrl {
  private static _inst: GameCtrl;

  public static get inst(): GameCtrl {
    if (!this._inst) {
      this._inst = new GameCtrl();
    }
    return this._inst;
  }

  public state: GameState; //控制客户端 操作 显示逻辑
  public gameInit(): void {
    this.state = GameState.Loading;

    //this.createPlayer();
    //建立网络连接
    //118.25.36.178:10000
    //10.10.113.61
    this.socket = new WebSocketClient('10.10.113.61');

    ViewMgr.ins.addEventListener(UICommandKey.gameEnd, this.gameEnd, this);
  }

  /**
   *
   */
  public socket: WebSocketClient;
  public startGame(name: string): void {
    this.socket.findGame(name);
  }
  public mapInit(): void {
    let rows: number = GameSet.rows;
    let cols: number = GameSet.cols;
    MapModel.ins.initMapData(rows, cols);
  }

  /**
   * 我的roleid
   */
  public myRole: string;
  /**
   * 初始化玩家信息
   */
  public players: Player[];
  public createPlayer(data: any[]): void {
    this.players = [];
    for (let i: number = 0; i < data.length; i++) {
      let pdata: any = data[i];
      let p1: Player = new Player();
      p1.init(pdata.id, pdata.id);
      p1.positionSeed = pdata.positionSeed;
      this.players.push(p1);
      if (i == 0) {
        this.myRole = p1.hid;
      }
    }

    for (let i: number = 0; i < this.players.length; i++) {
      let p: Player = this.players[i];
      p.color = parseInt(p.hid, 10) % 2;
      p.nowPos = this.createPlayerPos(p.positionSeed, p.hid);
    }
  }

  getEnemyplayer(hid: string): Player {
    for (let i = 0; i < this.players.length; i++) {
      if (hid !== this.players[i].hid) {
        return this.players[i];
      }
    }
  }

  /**
   * 创建角色初始位置
   */

  public createPlayerPos(ran: number, id: string): PosInfo {
    let pos: any = {};
    let canCreate: string[] = MapModel.ins.canCreatePlayerPos;
    // console.log(`${id}: `, ranIndex)
    let ranIndex: number = Math.floor(canCreate.length * ran);
    // 出现 bug 极大变小
    let canCreatePos: string = canCreate[ranIndex];

    pos.x = parseInt(canCreatePos.split('_')[1]);
    pos.y = parseInt(canCreatePos.split('_')[0]);
    return pos;
  }

  /**
   * 操作输入
   */
  public playerInput(input: PlayerInput): void {
    //此处提交给服务器
    //net.c2s({hid:this.myRole,action:input});
    this.socket.sendOperation({ input: input });
    /**
     * 测试代码
     */
    //ViewMgr.ins.emit(UICommandKey.onGetFrame,[{eid:ServerMessage.C2S_PlayerInput,hid:this.myRole,input:[input]}]);
  }

  /**
   * 操作取消
   */
  public playerCancel(input: PlayerInput): void {
    //此处提交给服务器
    //net.c2s({hid:this.myRole,action:input});
    this.socket.sendOperation({ cancel: input });
    /**
     * 测试代码
     */
    //ViewMgr.ins.emit(UICommandKey.onGetFrame,[{eid:ServerMessage.C2S_PlayerCancel,hid:this.myRole,input:[input]}]);
  }

  public gameEnd({ data }) {
    if (data.winPlayer === gameData.myPlayer.hid) {
      data.isWin = true;
      // @ts-ignore
      window.ResultData = data;
      cc.director.loadScene('Result');
    } else {
      data.isWin = false;
      // @ts-ignore
      window.ResultData = data;
      cc.director.loadScene('Result');
    }
  }

  /**
   * 拿到一帧操作
   */
  public getFramesData(): any[] {
    const { frameList } = this.socket;
    return frameList.splice(0, frameList.length);
  }
}
